<div style="margin: 5px;">
    <table id="borrow-table" lay-filter="borrow-table-filter"></table>
</div>

<script type="text/html" id="borrowBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">归还</a>
</script>
<script type="text/html" id="time-tpl">
    {{timestampToTime(d.borrow_time)}}
</script>

<script type="text/html" id="rtime-tpl">
    {{timestampToTime(d.borrow_return_time)}}

</script>

<script>

    function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
        D = date.getDate() + ' ';
        h = date.getHours() + ':';
        m = date.getMinutes() + ':';
        s = date.getSeconds();
        return Y+M+D+h+m+s;
    }


    layui.use(['table', 'form'], function () {
        var table = layui.table;
        //-------会员管理表格s--------
        var user = table.render({
            elem: '#borrow-table'
            , height: 'full-180'
            , url: '/admin/borrow/ajax'
            , page: true
            , cols: [[
                 {field: 'borrow_id', title: 'ID',align:'center'}
                ,{field: 'user_id', title: '借阅用户',align:'center'}
                , {field: 'book_id', title: '借阅书籍',align:'center'}
                , {field: 'borrow_time', title: '借出时间',align:'center',templet:'#time-tpl'}
                , {field: 'borrow_return_time', title: '归还时间',align:'center' ,templet:'#rtime-tpl'}
                , {field: '', title: '操作', toolbar: '#borrowBar',align:'center'}
            ]]
        });
        table.on('tool(borrow-table-filter)', function (obj) {
            let data = obj.data;
            if (obj.event == 'del') {

                layer.confirm('确认要将<span style="color:green">' + data.user_id + '</span>借的<br><span style="color:red">'+data.book_id+'</span>><br>这本书归还?', function (index) {
                    $.ajax({
                        type: 'post'
                        , url: '/admin/borrow/dell'
                        , dataType: 'json'
                        , data: {borrow_id: data.borrow_id}
                        , success: function (obj1) {
                            if (obj1.code == 1) {
                                obj.del();
                            }
                            layer.alert(obj1.msg);
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event == 'edit') {
                layer.open({
                    type: 2 //Page层类型
                    , area: ['550px', '600px']
                    , title: '修改会员'
                    , shade: 0.6 //遮罩透明度
                    , maxmin: true //允许全屏最小化
                    , anim: 1 //0-6的动画形式，-1不开启
                    , content: '/admin/usermanagement/edit?user_id=' + data.user_id
                    , end: function () {
                        user.reload('user-table');

                    }
                });
            }
        });
        var $ = layui.$, active = {
            addSort: function () { //获取选中数据
                layer.open({
                    type: 2 //Page层类型
                    , area: ['550px', '600px']
                    , title: '添加会员'
                    , shade: 0.6 //遮罩透明度
                    , maxmin: true //允许全屏最小化
                    , anim: 1 //0-6的动画形式，-1不开启
                    , content: '/admin/usermanagement/adduser'
                    , end: function () {
                        user.reload('user-table');

                    }
                });
            }
        };
        $('.userTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        //-------会员管理表格e--------



    });
</script>

